package com.chenshu.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

/**
 * Created with IntelliJ IDE
 * Description：常见类的使用
 * User：chenshu
 * Date：2024-03-26
 * Time：19:54
 */
public class Reflection02 {
    public static void main(String[] args) throws ClassNotFoundException, IOException, NoSuchFieldException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //1.读写文件
        Properties properties = new Properties();
        properties.load(new FileInputStream("src/static/re.properties"));
        String classPathName = properties.getProperty("classPath");
        String methodName = properties.getProperty("method");

        //2.加载类
        Class clazz = Class.forName(classPathName);
        Object o = clazz.newInstance();

        //Field的使用
        Field nameField = clazz.getField("age");
        nameField.set(o, 30);
        System.out.println("修改age后的值为：" + nameField.get(o));

        //Constructor 的使用
        Constructor constructor1 = clazz.getConstructor();
        Constructor constructor2 = clazz.getConstructor(String.class);
        Object object = constructor1.newInstance();
        Object object1 = constructor2.newInstance("test");
        System.out.println(object);
        System.out.println(object1);


    }
}
